package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.lanou.dllo.findfood.adpter.MyListAdpter;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.database.MyDataBase;
import com.lanou.dllo.findfood.entity.Dardry;


import java.util.ArrayList;

/**
 * Created by dllo on 15/10/26.
 */
public class DiaryFristActvity extends Activity {
    Button bt, returnbtn;
    //    ListView lv;
    private SwipeMenuListView lv;//可以滑动的listview
    LayoutInflater inflater;//布局加载器
    ArrayList<Dardry> array;
    MyDataBase mdb;
    MyListAdpter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frist);
//日记列表
        lv = (SwipeMenuListView) findViewById(R.id.listView);
        bt = (Button) findViewById(R.id.saveBtn);
        returnbtn = (Button) findViewById(R.id.backbutton);
        returnbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiaryFristActvity.this.finish();
            }
        });
        inflater = getLayoutInflater();

        mdb = new MyDataBase(this);
        array = mdb.getArray();

        adapter = new MyListAdpter(inflater, array);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getApplicationContext(), DiarySecondAtivity.class);
                intent.putExtra("ids", array.get(position).getIds());
                startActivity(intent);
                DiaryFristActvity.this.finish();
            }
        });
//        //对日志的行监听  listView对行的监听方法
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                                           final int position, long id) {

                //建立一个对话框提示删除
                new AlertDialog.Builder(DiaryFristActvity.this)
                        .setTitle("删除")
                        .setMessage("确定要删除这条菜谱吗?")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub

                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                mdb.toDelete(array.get(position).getIds());//调用数据库删除的方法
                                array = mdb.getArray();
                                MyListAdpter adapter = new MyListAdpter(inflater, array);
                                lv.setAdapter(adapter);
                            }
                        })
                        .create().show();
                return true;
            }
        });
        //滑动删除之后的回调方法


        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getApplicationContext(), DiarySecondAtivity.class);
                startActivity(intent);
                DiaryFristActvity.this.finish();
            }
        });

        //建立滑动菜单
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                // create "open" item
                SwipeMenuItem openItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                        0xCE)));
                // set item width
                openItem.setWidth(90);
                // set item title
                openItem.setTitle("查看");
                // set item title fontsize
                openItem.setTitleSize(18);
                // set item title font color
                openItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(openItem);

                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(90);
                // set a icon
                deleteItem.setIcon(R.mipmap.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };

        // set creator
        lv.setMenuCreator(creator);

        lv.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        // open
                        break;
                    case 1:
                        mdb.toDelete(array.get(position).getIds());//调用数据库删除的方法
                        array = mdb.getArray();
                        MyListAdpter adapter = new MyListAdpter(inflater, array);
                        lv.setAdapter(adapter);
                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });


    }


}
